<?php namespace Phpcmf;
/**
 * 用于兼容Phpcmf框架中的官方组件，官方组件可以通用于Poscms
 */
class Service
{

    /**
     * @var array
     */
    static private $instances = array();

    /**
     * @var object
     */
    static private $view;

    /**
     * @var object
     */
    static private $model;

    /**
     * 控制器对象实例
     *
     * @var object
     */
    public static function C() {
        return \get_instance();
    }

    // 是否是电脑端
    public static function IS_PC() {
        return !static::C()->is_mobile;
    }

    // 是否是移动端
    public static function IS_MOBILE() {
        return static::C()->is_mobile;
    }


    // 是否是开启过某项功能
    // \Poscms\Service::IS_OPEN('member')
    public static function IS_OPEN($name) {

        switch ($name) {
            case 'member':// 是否开启过会员功能
                if (!defined('SYS_CLOSE_MEMBER') || !SYS_CLOSE_MEMBER) {
                    return 1;
                }
                break;
            case 'NYRTJ':// 是否开启功能
                if (!defined('SYS_CLOSE_NYRTJ') || !SYS_CLOSE_NYRTJ) {
                    return 1;
                }
                break;
        }

        return 0;
    }

    /**
     * 模板视图对象实例
     *
     * @var object
     */
    public static function V() {

        if (!is_object(static::$view)) {
            static::$view = \get_instance()->template;
        }

        return static::$view;
    }

    /**
     * 模型类对象实例
     *
     * @var object
     */
    public static function model() {
        return static::C();
    }

    /**
     * 类对象实例
     *  \Poscms\Service::L('dcache');
     * @var object
     */
    public static function L( $name,  $namespace = '') {

        $params = null;
        if (is_array($namespace)){
            $params = $namespace;
            $namespace = '';
        }

        $name = strtolower($name);
        $_cname = 'PHPCMF-LIB'.md5($namespace.$name);
        $file1 = CMSPATH . 'fcms/library/'.ucfirst($name).'.php';

        if (!isset(static::$instances[$_cname]) or !is_object(static::$instances[$_cname])) {
            if (is_file($file1)) {
                require $file1;
            } else {
                show_error('Phpcmf Service无法加载类: /fcms/library/'.ucfirst($name).'.php');
            }
            $class_name = '\\Phpcmf\\Library\\'.ucfirst($name);
            if ($params) {
                static::$instances[$_cname] =  new $class_name($params);
            } else {
                static::$instances[$_cname] =  new $class_name();
            }
        }

        return static::$instances[$_cname];
    }

    /**
     * 模型对象实例
     *  \Poscms\Service::M('member');
     * @var object
     */
    public static function M( $name = '',  $namespace = '') {

        if (!isset(static::$instances['Phpcmf']) or !is_object(static::$instances['Phpcmf'])) {
            static::$instances['Phpcmf'] = new \Phpcmf\Model();
        }

        if (!$name) {
            return static::$instances['Phpcmf'];
        }

        $_cname = 'model-'.md5($namespace.$name);

        if (!isset(static::$instances[$_cname]) or !is_object(static::$instances[$_cname])) {

            if ($namespace) {
                $class = 'Phpcmf\\Model\\'.ucfirst($namespace).'\\'.ucfirst($name);
                $file = dr_get_app_dir($namespace).'Models/'.ucfirst($name).'.php';
            } else {
                $file = CMSPATH.'fcms/model/'.ucfirst($name).'.php';
                $class = 'Phpcmf\\Model\\'.ucfirst($name);
            }

            if (is_file($file)) {
                require_once $file;
            } else {
                show_error('Phpcmf Service无法加载模型: '.ucfirst($file));
            }

            static::$instances[$_cname] = new $class();
        }

        return static::$instances[$_cname];

    }

}